<template>
<div style="width: 400px;text-align:center;margin: 5px auto">
  <h1 STYLE="margin-bottom: 10px;">工程参数编辑</h1>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="参数类型" >
      <el-select v-model="ruleForm.propertyType" placeholder="请选择">
        <el-option
          v-for="item in this.propertyTypeConst"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="参数标题" >
      <el-input type="text" v-model="ruleForm.propertyTitle" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="宽度" >
      <el-input type="text" v-model="ruleForm.width" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="验证规则" >
      <el-input type="textarea" :rows="4" v-model="ruleForm.rules" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="选择内容" >
      <el-input type="text" v-model="ruleForm.options" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="参数名" >
      <el-input type="text" v-model="ruleForm.bindModel" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="占位符" >
      <el-input type="text" v-model="ruleForm.placeholder" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="最大长度" >
      <el-input type="text" v-model="ruleForm.maxLength" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="cs风格" >
      <el-input type="text" v-model="ruleForm.style" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="初始化值" >
      <el-input type="text" v-model="ruleForm.initVal" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="最大值" >
      <el-input type="text" v-model="ruleForm.maxVal" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="最小值" >
      <el-input type="text" v-model="ruleForm.minVal" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="小数位" >
      <el-input type="text" v-model="ruleForm.precision" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="步长" >
      <el-input type="text" v-model="ruleForm.step" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="最大行数" >
      <el-input type="text" v-model="ruleForm.maxRows" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="最小行数" >
      <el-input type="text" v-model="ruleForm.minRows" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="初始化行数" >
      <el-input type="text" v-model="ruleForm.initRowNum" autocomplete="off"  ></el-input>
    </el-form-item>
    <el-form-item label="初始化列数" >
      <el-input type="text" v-model="ruleForm.initColumnNum" autocomplete="off" @keyup.native="autoSubmit($event)" ></el-input>
    </el-form-item>
    <el-form-item >
      <div style="text-align:center">
        <el-button type="primary" v-if="allModify==='1'" @click="submitForm('ruleForm')">保存</el-button>
        <el-button v-if="allModify==='1'" @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="returnList">返回</el-button>
      </div>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
import request from "../utils/request";
import {propertyType} from "../const/const";

export default {
  name: "edit",
  data(){

    return{
      ruleForm: {
        id: '',
        projectId: '',
        propertyType: '',
        propertyTitle: '',
        width: '',
        rules: '',
        options: '',
        bindModel: '',
        placeholder: '',
        maxLength: '',
        style: '',
        initVal: '',
        maxVal: '',
        minVal: '',
        precision: '',
        step: '',
        maxRows: '',
        minRows: '',
        initRowNum: '',
        initColumnNum: '',
      },
      propertyTypeConst:null,
      allModify:'0',
      rules: {
      }
    }

  },
  methods: {
    autoSubmit: function (ev) {
      if (ev.code === "Enter" && this.allModify==='1') {
        this.submitForm('ruleForm');
      }
    },
    submitForm(formName) {
      let _this = this;
      const param = {
        id: _this.ruleForm.id,
        projectId: _this.ruleForm.projectId,
        propertyType: _this.ruleForm.propertyType,
        propertyTitle: _this.ruleForm.propertyTitle,
        width: _this.ruleForm.width,
        rules: _this.ruleForm.rules,
        options: _this.ruleForm.options,
        bindModel: _this.ruleForm.bindModel,
        placeholder: _this.ruleForm.placeholder,
        maxLength: _this.ruleForm.maxLength,
        style: _this.ruleForm.style,
        initVal: _this.ruleForm.initVal,
        maxVal: _this.ruleForm.maxVal,
        minVal: _this.ruleForm.minVal,
        precision: _this.ruleForm.precision,
        step: _this.ruleForm.step,
        maxRows: _this.ruleForm.maxRows,
        minRows: _this.ruleForm.minRows,
        initRowNum: _this.ruleForm.initRowNum,
        initColumnNum: _this.ruleForm.initColumnNum,
      };
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            method: "POST",
            url: "/api/ProjectParameter/update",
            data: param
          })
            .then(res => {
              if (res.data.status == "1") {
                this.$message(res.data.message);
                _this.$router.push({path: "/projectparameter/list/"+this.ruleForm.projectId});
              } else {
                this.$alert(res.data.message, {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
                return false;
              }
            })
            .catch(err => {
              debugger;
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadData(id){
      const  that = this;
      request({
        method: "POST",
        url: "/api/ProjectParameter/getById?id="+id,
      })
        .then(res => {
          if (res.data.status == "1") {
            that.ruleForm.id =  res.data.bean.ID;
            that.ruleForm.projectId =  res.data.bean.PROJECT_ID;
            that.ruleForm.propertyType =  res.data.bean.PROPERTY_TYPE;
            that.ruleForm.propertyTitle =  res.data.bean.PROPERTY_TITLE;
            that.ruleForm.width =  res.data.bean.WIDTH;
            that.ruleForm.rules =  res.data.bean.RULES;
            that.ruleForm.options =  res.data.bean.OPTIONS;
            that.ruleForm.bindModel =  res.data.bean.BIND_MODEL;
            that.ruleForm.placeholder =  res.data.bean.PLACEHOLDER;
            that.ruleForm.maxLength =  res.data.bean.MAX_LENGTH;
            that.ruleForm.style =  res.data.bean.STYLE;
            that.ruleForm.initVal =  res.data.bean.INIT_VAL;
            that.ruleForm.maxVal =  res.data.bean.MAX_VAL;
            that.ruleForm.minVal =  res.data.bean.MIN_VAL;
            that.ruleForm.precision =  res.data.bean.PRECISION;
            that.ruleForm.step =  res.data.bean.STEP;
            that.ruleForm.maxRows =  res.data.bean.MAX_ROWS;
            that.ruleForm.minRows =  res.data.bean.MIN_ROWS;
            that.ruleForm.initRowNum =  res.data.bean.INIT_ROW_NUM;
            that.ruleForm.initColumnNum =  res.data.bean.INIT_COLUMN_NUM;
            this.$message(res.data.message);
          } else {
            this.$alert(res.data.message, {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
            return false;
          }
        })
        .catch(err => {
          debugger;
          console.log(err);
        });
    },
    returnList(){
      this.$router.push({path: "/projectparameter/list/"+this.ruleForm.projectId});
    }
  },
  mounted() {
    console.log("ID:"+this.$route.params.ID);
    this.ruleForm.id = this.$route.params.id;
    this.loadData(this.ruleForm.id);
    this.propertyTypeConst  = propertyType;
    this.allModify = localStorage.allowProjectModify;
  }
}
</script>

<style scoped>

</style>
